local yizhao = fk.CreateSkill {

  name = "joy__yizhao",

  tags = { Skill.Compulsory, },

}



yizhao:addEffect(fk.CardUsing, {
  name = "joy__yizhao",
  anim_type = "drawcard",
  frequency = Skill.Compulsory,
  events = {fk.CardUsing, fk.CardResponding},
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(yizhao.name) and data.card.number > 0
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local n1 = tostring(player:getMark("@joy__zhangjiao_huang"))
    room:addPlayerMark(player, "@joy__zhangjiao_huang", math.min(data.card.number, #Fk:getAllCardIds() - player:getMark("@joy__zhangjiao_huang")))
    local n2 = tostring(player:getMark("@joy__zhangjiao_huang"))
    if n1//10 == n2//10 then 
      return
    end
    local x = (n2%10)//10
    if x == 0 then x = 10 end  --yes, tenyear is so strange
    local card = room:getCardsFromPileByRule(".|"..x)
    if #card > 0 then
      room:moveCards({
        ids = card,
        to = player.id,
        toArea = Card.PlayerHand,
        moveReason = fk.ReasonJustMove,
        proposer = player.id,
        skillName = yizhao.name,
      })
    end
  end,
})
yizhao:addEffect(fk.CardResponding, {
  name = "joy__yizhao",
  anim_type = "drawcard",
  frequency = Skill.Compulsory,
  events = {fk.CardUsing, fk.CardResponding},
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(yizhao.name) and data.card.number > 0
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local n1 = tostring(player:getMark("@joy__zhangjiao_huang"))
    room:addPlayerMark(player, "@joy__zhangjiao_huang", math.min(data.card.number, #Fk:getAllCardIds() - player:getMark("@joy__zhangjiao_huang")))
    local n2 = tostring(player:getMark("@joy__zhangjiao_huang"))
    if n1//10 == n2//10 then 
      return
    end
    local x = (n2%10)//10
    if x == 0 then x = 10 end  --yes, tenyear is so strange
    local card = room:getCardsFromPileByRule(".|"..x)
    if #card > 0 then
      room:moveCards({
        ids = card,
        to = player.id,
        toArea = Card.PlayerHand,
        moveReason = fk.ReasonJustMove,
        proposer = player.id,
        skillName = yizhao.name,
      })
    end
  end,
})
yizhao:addEffect(fk.EventLoseSkill, {
  refresh_events = {fk.EventLoseSkill},
  can_refresh = function(self, event, target, player, data)
    return player == target and data == self and player:getMark("@joy__zhangjiao_huang") ~= 0
  end,
  on_refresh = function(self, event, target, player, data)
    player.room:setPlayerMark(player, "@joy__zhangjiao_huang", 0)
  end,
})

return yizhao